<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <%@include file="../_static/_head.jsp"%>
</head>
<body>
<article class="page-container">
    <form class="form form-horizontal" id="form">
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>上传标题图片</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="hidden" class="input-text" id="titleImg" name="titleImg">
                <button type="button" class="layui-btn" id="uploadImg">上传图片</button>
                <div class="layui-upload-list">
                    <img class="layui-upload-img" id="showImg">
                    <p id="uploadImgMsg"></p>
                </div>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>标题：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" autocomplete="off" id="title" name="title">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>内容：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="hidden" id="content" name="content">
                <textarea class="layui-textarea" id="contentTemp" name="contentTemp" style="display: none">
                </textarea>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>排序：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="number" class="input-text" autocomplete="off" id="sort" name="sort" value="0">
            </div>
        </div>
        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <input class="btn btn-primary radius" type="submit" value="提交">
            </div>
        </div>
    </form>
</article>
<%@include file="../_static/_foot.jsp"%>
<script>
    $("#form").validate({
        rules:{
            title:{
                required:true,
            },
            sort:{
                required:true,
            }
        },
        onkeyup:false,
        focusCleanup:true,
        success:"valid",
        submitHandler:function(form){
            var text=layedit.getText(index);
            console.log(text);
            $("#content").val(text);
            $(form).ajaxSubmit({
                type: 'POST',
                url: "/information/news/saveupdate.json" ,
                success: function(data){
                    if (data.code==200){
                        window.parent.location.reload();
                        layer.msg('添加成功!',{icon:1,time:1000});
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    } else{
                        layer.msg(data.msg,{icon:2,time:1000});
                    }
                },
                error: function(XmlHttpRequest, textStatus, errorThrown){
                    layer.msg('错误!',{icon:2,time:1000});
                }
            });

        }
    });

    layui.use(['upload','layedit'], function(){
        var $ = layui.jquery
            ,upload = layui.upload
            ,layedit = layui.layedit;

        layedit.set({
            uploadImage: {
                url: '/uploadLayedit.json' //接口url
                , type: 'post' //默认post
            }
        });

        //构建一个默认的编辑器
        var index = layedit.build('contentTemp');

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#uploadImg'
            ,url: '/upload.json'
            ,accept: 'file'
            ,size: 1024
            ,before: function(obj){
                obj.preview(function(index, file, result){
                    $('#showImg').attr('src', result);
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code ==500){
                    return layer.msg(res.msg);
                }else{
                    $("#titleImg").val(res.msg);
                }
                //上传成功
            }
            ,error: function(){
                var demoText = $('#uploadImgMsg');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs upload-reload">重试</a>');
                demoText.find('.upload-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });
    });
</script>
</body>
</html>
